#region Copyright (c) 2003 - 2006 Toenda Software Development
/*
 * Copyright (c) 2003 - 2006 Toenda Software Development
 */
#endregion

using MSSystem = System;
using System.Data;
using System.Collections;
using System.Text;
using Toenda.Rails.Data;
using Toenda.Rails.DataContainer;

namespace Toenda.Rails {
	namespace System {
		/// <summary>
		/// Class StaticProvider
		/// </summary>
		public class StaticProvider : DBClient {
			/// <summary>
			/// Default constructor
			/// </summary>
			public StaticProvider() {
			}

			/// <summary>
			/// Get a arraylist of static items
			/// </summary>
			/// <param name="Tag"></param>
			/// <returns></returns>
			public ArrayList GetStaticItems(string Tag) {
				ArrayList arr = new ArrayList();
				DataSet dsData = base.GetStatics(Tag, OrderStaticsBy.Undefined);

				if(dsData.Tables[0].Rows.Count > 0){
					DataTableReader oDT = dsData.CreateDataReader();
					
					while(oDT.Read()){
						Static stc = this._FillStaticFromTableReader(oDT);
						arr.Add(stc);
					}
				}

				return arr;
			}

			/// <summary>
			/// Get a arraylist of static items
			/// </summary>
			/// <param name="Tag"></param>
			/// <param name="osb"></param>
			/// <returns></returns>
			public ArrayList GetStaticItems(string Tag, OrderStaticsBy osb) {
				ArrayList arr = new ArrayList();
				DataSet dsData = base.GetStatics(Tag, osb);

				if(dsData.Tables[0].Rows.Count > 0) {
					DataTableReader oDT = dsData.CreateDataReader();

					while(oDT.Read()) {
						Static stc = this._FillStaticFromTableReader(oDT);
						arr.Add(stc);
					}
				}

				return arr;
			}

			/// <summary>
			/// Get a single static item
			/// </summary>
			/// <param name="Tag"></param>
			/// <param name="Value"></param>
			/// <returns></returns>
			public Static GetStaticItem(string Tag, string Value) {
				DataSet dsData = base.GetStatic(Tag, Value);
				Static stc = new Static();

				if(dsData.Tables[0].Rows.Count > 0) {
					DataTableReader oDT = dsData.CreateDataReader();
					oDT.Read();

					stc = this._FillStaticFromTableReader(oDT);
				}

				return stc;
			}

			/// <summary>
			/// [PRIVATE] Fill a static from table reader
			/// </summary>
			/// <param name="oDT"></param>
			/// <returns></returns>
			private Static _FillStaticFromTableReader(DataTableReader oDT) {
				Static stc = new Static();

				stc.Tag = (oDT.IsDBNull(oDT.GetOrdinal("tag")) ? "" : oDT["tag"].ToString());
				stc.Type = (oDT.IsDBNull(oDT.GetOrdinal("type")) ? "" : oDT["type"].ToString());
				stc.Content = (oDT.IsDBNull(oDT.GetOrdinal("content")) ? "" : oDT["content"].ToString());
				stc.Value = (oDT.IsDBNull(oDT.GetOrdinal("value")) ? "" : oDT["value"].ToString());
				stc.Length = oDT.IsDBNull(oDT.GetOrdinal("length")) ? 0 : oDT.GetInt32(oDT.GetOrdinal("length"));

				return stc;
			}

			/// <summary>
			/// [PRIVATE] Fill a static from table reader
			/// </summary>
			/// <param name="oDT"></param>
			/// <returns></returns>
			private Static _FillStaticFromTableReader(DataTableReader oDT, string State) {
				Static stc = new Static();
				DataSet dsData = new DataSet();

				stc.Tag = (oDT.IsDBNull(oDT.GetOrdinal("tag")) ? "" : oDT["tag"].ToString());
				stc.Type = (oDT.IsDBNull(oDT.GetOrdinal("type")) ? "" : oDT["type"].ToString());
				stc.Content = (oDT.IsDBNull(oDT.GetOrdinal("content")) ? "" : oDT["content"].ToString());
				stc.Value = (oDT.IsDBNull(oDT.GetOrdinal("value")) ? "" : oDT["value"].ToString());
				stc.Length = oDT.IsDBNull(oDT.GetOrdinal("length")) ? 0 : oDT.GetInt32(oDT.GetOrdinal("length"));

				return stc;
			}
		}
	}
}
